<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2005/10/24 22:15:35 $)
// $RCSfile: staff_misc.php,v $ : $Revision: 1.6 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

if ($_SWIFT["staff"]["kb_entab"] == "0")
{
	echo '<font face="Verdana, Arial, Helvetica" size="2" color="red"><b>'.$_SWIFT["language"]["nopermission"].'</b></font>';
	exit;
}

require_once ("./modules/knowledgebase/functions_knowledgebase.php");
require_once ("./includes/functions_html.php");
require_once ("./includes/functions_users.php");
require_once ("./includes/functions_jscache.php");
require_once ("./includes/functions_template.php");
require_once ("./includes/data_mimelist.php");
$template->loadLanguageSection("knowledgebase");

$interface->assignQuickLink($_SWIFT["language"]["managecategories"], "index.php?_m=knowledgebase&_a=managecategories", "icon_list.gif");
$interface->assignQuickLink($_SWIFT["language"]["managequestions"], "index.php?_m=knowledgebase&_a=managequestions", "icon_topicyellow.gif");
$interface->assignQuickLink($_SWIFT["language"]["insertcategory"], "index.php?_m=knowledgebase&_a=insertcategory", "icon_newcategory.gif");
$interface->assignQuickLink($_SWIFT["language"]["insertquestion"], "index.php?_m=knowledgebase&_a=insertquestion", "icon_inserttopic.gif");

renderCategoryList();


/**
* ###############################################
* PREVIEW
* ###############################################
*/
if ($eventaction == "preview")
{
	$_kbarticle = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."kbarticles` AS kbarticles LEFT JOIN `". TABLE_PREFIX ."kbarticledata` AS kbarticledata ON (kbarticles.kbarticleid = kbarticledata.kbarticleid) WHERE kbarticles.kbarticleid = '". intval($_GET["kbarticleid"]) ."';");
	if (empty($_kbarticle["kbarticleid"]))
	{
		trigger_error($_SWIFT["language"]["invalidkbarticle"], E_USER_ERROR);
	}

	echo $template->displayTemplate("header.tpl");
	?>
				<table width="100%"  border="0" cellspacing="1" cellpadding="0">
                      <tr>
					  <td width="1" align="left"><img src="<?=$_SWIFT["themepath"]?>icon_topicbig.gif" align="absmiddle" border="0" /></td>
					  <td align="left" valign="middle" width="100%"><span class="articletitle">&nbsp;<?=$_kbarticle["subject"]?></span></td>
					  </tr>
					  <tr>
						<td width="100%" colspan="2"><img src="<?=$_SWIFT["themepath"]?>space.gif" width="1" height="4" border="0" /></td>
						</tr>
				  </table>

	<fieldset class="swiftfieldset">
	<legend><?=$_SWIFT["language"]["solution"]?></legend>
	<span class="mediumtext"><?=getProcessedHTML($_kbarticle["contents"])?></span><BR /><BR />
	</fieldset>
	<BR />
	<a href="javascript:window.close();" id="moduletitle"><?=$_SWIFT["language"]["closewindow"]?></a>
	<?
	echo $template->displayTemplate("footer.tpl");




/**
* ###############################################
* IMPORT/EXPORT
* ###############################################
*/
} else if ($eventaction == "importexport") {
	require_once ("./includes/functions_xml.php");
	$xml = new XML;
	require_once ("./modules/knowledgebase/functions_importexport.php");

	if ($_POST["step"] == "export")
	{
		// Export the categories
		exportKBCategories($_POST["kbcategoryid"]);
		exit;
	} else if ($_POST["step"] == "import") {
		if (file_exists($_FILES["kbfile"]["tmp_name"]))
		{
			importKBData($_FILES["kbfile"]["tmp_name"]);
			printRedirect($_SWIFT["language"]["importconfirm"], "index.php?_m=knowledgebase&_a=managequestions&importconfirm=1");
			exit;
		} else {
			$errormessage = $_SWIFT["language"]["importfileerror"];
		}
	}
	

	$interface->staffHeader($_SWIFT["language"]["knowledgebase"]." &gt; ".$_SWIFT["language"]["importexport"], 4);

	$interface->staffNavBar('<a href="index.php?_m=knowledgebase&_a=managequestions" title="'.$_SWIFT["language"]["managequestions"].'">'.$_SWIFT["language"]["managequestions"].'</a> &raquo; '.'<a href="index.php?_m=knowledgebase&_a=importexport" title="'.$_SWIFT["language"]["importexport"].'">'.$_SWIFT["language"]["importexport"].'</a>', "", 4);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	// ======= EXPORT KNOWLEDGEBASE =======
	$categorylist = array();
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."kbcategories` WHERE `parentcategoryid` = '0' ORDER BY `displayorder` ASC;");
	while ($dbCore->nextRecord())
	{
		$categorylist[] = $dbCore->Record;
	}

	if (count($categorylist))
	{
		printFormStart();
		printMainTableHeader($_SWIFT["language"]["export"]);

		$options = array();
		$options[0]["value"] = 0;
		$options[0]["title"] = $_SWIFT["language"]["exportparentcat"];
		$options[0]["checked"] = true;
		$index = 1;
		foreach ($categorylist as $key=>$val)
		{
			$options[$index]["value"] = $val["kbcategoryid"];
			$options[$index]["title"] = $val["title"];
			$options[$index]["checked"] = true;
			$index++;
		}

		printCheckboxListRow("kbcategoryid", $_SWIFT["language"]["kbexportcat"], $_SWIFT["language"]["desc_kbexportcat"], $options);

		printSubmitRow($_SWIFT["language"]["exportxml"]);

		printMainTableFooter();

		addHiddenField("_m", "knowledgebase");
		addHiddenField("_a", "importexport");
		addHiddenField("step", "export");
		printFormEnd();
		echo "<BR />";
	}

	// ======= IMPORT KNOWLEDGEBASE =======
	printFormStart("importform", "index.php", "POST", true);
	printMainTableHeader($_SWIFT["language"]["import"]);

	printUploadRow("kbfile", $_SWIFT["language"]["kbfile"], $_SWIFT["language"]["desc_kbfile"]);

	printSubmitRow($_SWIFT["language"]["importxml"]);

	printMainTableFooter();

	addHiddenField("_m", "knowledgebase");
	addHiddenField("_a", "importexport");
	addHiddenField("step", "import");
	printFormEnd();

	$template->assign("backurl", "index.php?_m=knowledgebase&_a=managequestions");

	$interface->staffFooter();
}
?>